|
1
|
|
|
import { assigned } from '../utils/helpers'; |
|
2
|
|
|
|
|
3
|
|
|
enum BrowserApi { |
|
4
|
|
|
Unknown, |
|
5
|
|
|
Chromium, |
|
6
|
|
|
Firefox, |
|
7
|
|
|
} |
|
8
|
|
|
|
|
9
|
|
|
export type ConfigurationStorageBooleanCallback = (value: boolean) => void; |
|
10
|
|
|
|
|
11
|
|
|
export interface ICustomBrowserStorageProvider { |
|
12
|
|
|
setDataAsBoolean(key: string, value: boolean): void; |
|
13
|
|
|
getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
class StorageProviderChromium implements ICustomBrowserStorageProvider { |
|
17
|
|
|
|
|
18
|
|
|
setDataAsBoolean(key: string, value: boolean): void { |
|
19
|
|
|
let obj = {}; |
|
20
|
|
|
obj[key] = value; |
|
21
|
|
|
|
|
22
|
|
|
this.getStorage().set(obj); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void { |
|
26
|
|
|
this.getStorage().get(key, items => { |
|
27
|
|
|
if (assigned(items) && assigned(items[key])) { |
|
28
|
|
|
callback(items[key]); |
|
29
|
|
|
} |
|
30
|
|
|
else { |
|
31
|
|
|
callback(defaultValue); |
|
32
|
|
|
} |
|
33
|
|
|
}) |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private getStorage(): chrome.storage.StorageArea { |
|
37
|
|
|
if (assigned(chrome.storage.sync)) { |
|
38
|
|
|
return chrome.storage.sync; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return chrome.storage.local; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
class StorageProviderFirefox implements ICustomBrowserStorageProvider { |
|
47
|
|
|
|
|
48
|
|
|
setDataAsBoolean(key: string, value: boolean): void { |
|
49
|
|
|
let obj = {}; |
|
50
|
|
|
obj[key] = value; |
|
51
|
|
|
|
|
52
|
|
|
this.getStorage().set(obj); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
getDataAsBoolean(key: string, defaultValue: boolean, callback: ConfigurationStorageBooleanCallback): void { |
|
56
|
|
|
let promise = this.getStorage().get(key); |
|
57
|
|
|
|
|
58
|
|
|
promise.then(items => { |
|
59
|
|
|
if (assigned(items) && assigned(items[key])) { |
|
60
|
|
|
callback(items[key] as boolean); |
|
61
|
|
|
} |
|
62
|
|
|
else { |
|
63
|
|
|
callback(defaultValue); |
|
64
|
|
|
} |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private getStorage(): browser.storage.StorageArea { |
|
69
|
|
|
if (assigned(browser.storage.sync)) { |
|
70
|
|
|
return browser.storage.sync; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return browser.storage.local; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
let __storageProvieder: ICustomBrowserStorageProvider; |
|
78
|
|
|
|
|
79
|
|
|
function getBrowserApi(): BrowserApi { |
|
80
|
|
|
if (typeof chrome !== 'undefined') { |
|
81
|
|
|
if (typeof browser !== 'undefined') { |
|
82
|
|
|
return BrowserApi.Firefox; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return BrowserApi.Chromium; |
|
86
|
|
|
} |
|
87
|
|
|
else if (typeof browser !== 'undefined') { |
|
88
|
|
|
return BrowserApi.Firefox; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return BrowserApi.Unknown; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
function createStorageProvider() { |
|
95
|
|
|
|
|
96
|
|
|
let api = getBrowserApi(); |
|
97
|
|
|
|
|
98
|
|
|
// chromium |
|
99
|
|
|
if (api === BrowserApi.Chromium) { |
|
100
|
|
|
__storageProvieder = new StorageProviderChromium(); |
|
101
|
|
|
} |
|
102
|
|
|
// firefox |
|
103
|
|
|
else if (api === BrowserApi.Firefox) { |
|
104
|
|
|
__storageProvieder = new StorageProviderFirefox(); |
|
105
|
|
|
} |
|
106
|
|
|
else { |
|
107
|
|
|
throw "Unknown browser API. Cannot create storage provider."; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
export function getGlobalStorageProvider(): ICustomBrowserStorageProvider { |
|
112
|
|
|
if (!assigned(__storageProvieder)) { |
|
113
|
|
|
createStorageProvider(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return __storageProvieder; |
|
117
|
|
|
} |